{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.cnblogs.com/vipstone/p/8884991.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''创建文件夹'''\n",
    "import os\n",
    "import glob\n",
    "import cv2\n",
    "\n",
    "def mkdir(path):\n",
    "    # 去除首位空格\n",
    "    path=path.strip()\n",
    "    # 去除尾部 \\ 符号\n",
    "    path=path.rstrip(\"\\\\\")\n",
    "    isExists=os.path.exists(path)\n",
    " \n",
    "    # 判断结果\n",
    "    if not isExists:\n",
    "        # 如果不存在则创建目录\n",
    "        print(path+' 创建成功')\n",
    "        # 创建目录操作函数\n",
    "        os.makedirs(path)\n",
    "        return True\n",
    "    else:\n",
    "        # 如果目录存在则不创建，并提示目录已存在\n",
    "        print(path+' 目录已存在')\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "faceImageGray/Huang Jingfeng 创建成功\n",
      "faceImageGray/yaleB11 创建成功\n",
      "faceImageGray/yaleB12 创建成功\n",
      "faceImageGray/yaleB13 创建成功\n",
      "faceImageGray/yaleB15 创建成功\n",
      "faceImageGray/yaleB16 创建成功\n",
      "faceImageGray/yaleB17 创建成功\n",
      "faceImageGray/yaleB18 创建成功\n",
      "faceImageGray/yaleB19 创建成功\n",
      "faceImageGray/yaleB20 创建成功\n",
      "faceImageGray/yaleB21 创建成功\n"
     ]
    }
   ],
   "source": [
    "# 循环读取图片\n",
    "filepaths = glob.glob(\"faceImages/*\")\n",
    "for filepath in filepaths:\n",
    "    files = glob.glob(filepath + \"/\" + '*.jpg') \n",
    "    path = \"faceImageGray/\"+filepath.split('\\\\')[-1]\n",
    "    mkdir(path)\n",
    "    for file in files:        \n",
    "        img = cv2.imread(file)\n",
    "        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "        # OpenCV人脸识别分类器\n",
    "        classifier = cv2.CascadeClassifier(\"haarcascades\\haarcascade_frontalface_default.xml\")#人脸分类模型xml\n",
    "    \n",
    "        # 定义绘制颜色\n",
    "        color = (0, 255, 0) \n",
    "\n",
    "        # 调用识别人脸\n",
    "        faceRects = classifier.detectMultiScale(\n",
    "            gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))\n",
    "    \n",
    "        # 大于0则检测到人脸\n",
    "        if len(faceRects):  \n",
    "            # 单独框出每一张人脸\n",
    "            for faceRect in faceRects:  \n",
    "                x, y, w, h = faceRect\n",
    "                # 框出人脸\n",
    "                cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)\n",
    "                # 左眼\n",
    "                cv2.circle(img, (x + w // 4, y + h // 4 + 30), min(w // 8, h // 8),color)\n",
    "                #右眼\n",
    "                cv2.circle(img, (x + 3 * w // 4, y + h // 4 + 30), min(w // 8, h // 8),color)\n",
    "                #嘴巴\n",
    "                cv2.rectangle(img, (x + 3 * w // 8, y + 3 * h // 4),\n",
    "                          (x + 5 * w // 8, y + 7 * h // 8), color)\n",
    "                #截取图片\n",
    "                src = img[y:y+w,x:x+h]\n",
    "                # 灰度化\n",
    "                src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)\n",
    "                src_gray = cv2.resize(src_gray, (64, 64))\n",
    "                \n",
    "                #存储切割出检测并灰度化的人脸图片\n",
    "                cv2.imwrite(path+'/'+file.split('\\\\')[-1],src_gray)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
